<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;
use think\db\Query;
use app\admin\model\ClienteleModel;

/**
 * Class UserController
 * @package app\admin\controller
 * @adminMenuRoot(
 *     'name'   => '管理组',
 *     'action' => 'default',
 *     'parent' => 'user/AdminIndex/default',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   => '',
 *     'remark' => '管理组'
 * )
 */
class ClienteleController extends AdminBaseController
{

    /**
     * 管理员列表
     * @adminMenu(
     *     'name'   => '管理员',
     *     'parent' => 'default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '管理员管理',
     *     'param'  => ''
     * )
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $content = hook_one('admin_user_index_view');

        if (!empty($content)) {
            return $content;
        }

        /**搜索条件**/
        $name = $this->request->param('name');
        $userEmail = '';
        $users = Db::name('clientele')
//            ->where('user_type', 1)
            ->where(function (Query $query) use ($name , $userEmail) {
                if ($name ) {
                    $query->where('name', 'like', "%$name%");
                }

                if ($userEmail) {
                    $query->where('user_email', 'like', "%$userEmail%");
                }
            })
            ->order("id DESC")
            ->paginate(10);
        $users->appends(['name' => $name, 'user_email' => $userEmail]);
        // 获取分页显示
        $page = $users->render();

        
        
        $this->assign("page", $page);
        
        $this->assign("users", $users);
        return $this->fetch();
    }

    /**
     * 管理员添加
     * @adminMenu(
     *     'name'   => '管理员添加',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '管理员添加',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $content = hook_one('admin_user_add_view');

        if (!empty($content)) {
            return $content;
        }

        $roles = Db::name('role')->where('status', 1)->order("id DESC")->select();
        $this->assign("roles", $roles);
        return $this->fetch();
    }

    /**
     * 管理员添加提交
     * @adminMenu(
     *     'name'   => '管理员添加提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '管理员添加提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $save_data = $data['post'];
            $result = $this->validate($save_data, 'Clientele.add');
            
            if ($result !== true) {                                
                $this->error($result);
            } else {
                if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
                    $save_data['more']['photos'] = [];
                    foreach ($data['photo_urls'] as $key => $url) {
                        $photoUrl = cmf_asset_relative_url($url);
                        array_push($save_data['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
                    }
                }

                if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                    $save_data['more']['files'] = [];
                    foreach ($data['file_urls'] as $key => $url) {
                        $fileUrl = cmf_asset_relative_url($url);
                        array_push($save_data['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
                    }
                }
                $save_data['more'] = json_encode($save_data['more']);
                $save_data['create_time'] = time();
         
                $result = DB::name('clientele')->insertGetId($save_data);
                if ($result !== false) {                       
                    $this->success("添加成功！", url("Clientele/index"));
                } else {
                    $this->error("添加失败！");
                }
            }

        }
    }

    /**
     * 管理员编辑
     * @adminMenu(
     *     'name'   => '管理员编辑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '管理员编辑',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $content = hook_one('admin_user_edit_view');

        if (!empty($content)) {
            return $content;
        }

        $id    = $this->request->param('id', 0, 'intval');
        
        $clienteleModel = new ClienteleModel();
        $post            = $clienteleModel->where('id', $id)->find();
        
//        $roles = DB::name('role')->where('status', 1)->order("id DESC")->select();
//        $this->assign("roles", $roles);
//        $role_ids = DB::name('RoleUser')->where("user_id", $id)->column("role_id");
//        $this->assign("role_ids", $role_ids);
        
        $this->assign('post',$post);
        return $this->fetch();
    }

    /**
     * 管理员编辑提交
     * @adminMenu(
     *     'name'   => '管理员编辑提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '管理员编辑提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $save_data = $data['post'];
//            $result = $this->validate($save_data, 'Team.edit');
            $result = TRUE;
          
            if ($result !== true) {                                
                $this->error($result);
            } else {
                if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
                    $save_data['more']['photos'] = [];
                    foreach ($data['photo_urls'] as $key => $url) {
                        $photoUrl = cmf_asset_relative_url($url);
                        array_push($save_data['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
                    }
                }

                if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                    $save_data['more']['files'] = [];
                    foreach ($data['file_urls'] as $key => $url) {
                        $fileUrl = cmf_asset_relative_url($url);
                        array_push($save_data['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
                    }
                }
                $id = $this->request->param('id', 0, 'intval');
                $save_data['more'] = json_encode($save_data['more']);
                
                $result = DB::name('clientele')->where('id',$id)->update($save_data);
             
                if ($result !== false) {                       
                    $this->success("添加成功！", url("Clientele/index"));
                } else {
                    $this->error("添加失败！");
                }
            }

        }
    }


    /**
     * 管理员删除
     * @adminMenu(
     *     'name'   => '管理员删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '管理员删除',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        // if ($id == 1) {
        //     $this->error("最高管理员不能删除！");
        // }

        if (Db::name('clientele')->delete($id) !== false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }
    
    
     public function listOrder()
    {
        parent::listOrders(Db::name('clientele'));
        $this->success("排序更新成功！", '');
    }

    
}